package com.xnyzc.lhy.common.component.config;

import com.alibaba.fastjson.JSONObject;
import com.xnyzc.lhy.common.component.didi.DiDiUrlConstant;
import com.xnyzc.lhy.common.entity.DiDiRv;
import com.xnyzc.lhy.common.entity.Rv;
import com.xnyzc.lhy.common.exception.BusinessSilentException;
import com.xnyzc.lhy.common.exception.PangException;
import com.xnyzc.lhy.common.penum.EDiDiErrorCode;
import com.xnyzc.lhy.common.penum.EErrorCode;
import com.xnyzc.lhy.common.util.CheckUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.NoHandlerFoundException;

import javax.servlet.http.HttpServletRequest;

/**
 * 公用错误拦截
 *
 * @Author: scootXin
 * @Date: 2018/12/5 15:26
 */
@Slf4j
@RestControllerAdvice
public class GlobalExceptionConfig {

    /**
     * 默认错误
     *
     * @param e
     * @return
     */
    @ExceptionHandler(Exception.class)
    @ResponseBody
    public Object handlerException(HttpServletRequest req, Exception e) {

        String url = req.getServletPath();
        if (CheckUtil.strIsNotEmpty(url) && url.startsWith(DiDiUrlConstant.requestServiceUrlPrefix)) {
            log.error("{} 请求异常,异常信息:{}", url, CheckUtil.strIsEmpty(e.getMessage()) ? "无异常信息" : e.getMessage());
            return DiDiRv.wrap(EDiDiErrorCode.dataException, EDiDiErrorCode.dataException.getDesc(), new JSONObject());
        } else if (e instanceof NoHandlerFoundException) {
            log.error("{} 请求异常,异常信息:{}", ((NoHandlerFoundException) e).getRequestURL(), CheckUtil.strIsEmpty(e.getMessage()) ? "无异常信息" : e.getMessage());
            return Rv.wrap(EErrorCode.dataException, EErrorCode.dataException.getDesc(), new JSONObject());
        } else if (e instanceof PangException) {
            PangException e1 = (PangException) e;
            log.error("{} 请求异常,异常信息:{}", url, CheckUtil.strIsEmpty(e1.getMessage()) ? "无异常信息" : e1.getMessage());
            return Rv.wrap(e1.getErrorCode(), CheckUtil.strIsEmpty(e1.getMessage()) ? "操作失败" : e1.getMeMessage(), new JSONObject());
        } else if (e instanceof AccessDeniedException) {
            log.error("{} 请求异常,异常信息:{}", url, CheckUtil.strIsEmpty(e.getMessage()) ? "无异常信息" : e.getMessage());
            return Rv.wrap(EErrorCode.dataException, EErrorCode.dataException.getDesc(), new JSONObject());
        } else if (e instanceof BusinessSilentException) {
            BusinessSilentException e1 = (BusinessSilentException) e;
            log.error("{} 请求异常,异常信息:{}", url, CheckUtil.strIsEmpty(e.getMessage()) ? "无异常信息" : e.getMessage());
            return Rv.wrap(e1.getCode(), e1.getDescription(), null);
        }else {
            log.error("{} 请求异常,异常信息:{}", url, CheckUtil.strIsEmpty(e.getMessage()) ? "无异常信息" : e.getMessage());
            return Rv.wrap(EErrorCode.dataException, EErrorCode.dataException.getDesc(), new JSONObject());
        }
    }

}
